
package com.itheima.reggie.common;
    //基于ThreadLocal封装的工具类，用来保存和获取当前登录用户id
    public class BaseContest {
        //threadlocal会为每个线程单独创建一个单独的空间
        private static final ThreadLocal<Long> threadLocal = new ThreadLocal<>();
        /**
         * 设置值
         * @param id
         */
        //将当前登录用户id存入ThreadLocal中
        public static void setCurrentId(Long id){
            threadLocal.set(id);
        }
        /**
         * 获取值
         * @return
         */
        //从ThreadLocal中获取当前登录用户的id
        public static Long getCurrentId(){
            return threadLocal.get();
        }

        //此方法会释放threadLocal的空间，避免后期因代码执行次数过多，导致内存溢出问题的出现
        public static void removeCurrentId(){
            threadLocal.remove();
        }
    }

